﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Collections;
namespace FTchina.Library
{

    /// <summary>
    ///StringHelper 的摘要说明
    /// </summary>
    public class StringHelper
    {
        public StringHelper()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }


        #region 随机数
        /// <summary>
        /// 获取指定长度的随机数
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string getRandomNumber(int length)
        {
            string result = "";
            System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            for (int i = 0; i < length; i++)
            {
                result += random.Next(10).ToString();
            }
            return result;
        }
        /// <summary>
        /// 获取指定长度的字母
        /// </summary>
        /// <param name="Length"></param>
        /// <returns></returns>
        public static string getRandomLetter(int Length)
        {

            char[] Pattern = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
            string result = "";
            int n = Pattern.Length;
            System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            for (int i = 0; i < Length; i++)
            {
                int rnd = random.Next(0, n);
                result += Pattern[rnd];
            }
            return result;
        }

        /// <summary>
        /// 获取指定长度的随机数字和字母
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string getRandomLetterAndNumber(int length)
        {

            char[] Pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
            string result = "";
            int n = Pattern.Length;
            System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            for (int i = 0; i < length; i++)
            {
                int rnd = random.Next(0, n);
                result += Pattern[rnd];
            }
            return result;
        }


        #endregion


        #region 数组 字符串的转换
        /// <summary>
        /// 将string[] 转换成string,string 
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static string getArrayString(string[] array)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string item in array)
            {
                sb.Append(item);
                sb.Append(",");
            }
            return sb.ToString().TrimEnd(',');
        }

        #endregion


        #region 获取时间字符串
        /// <summary>
        /// 获取当前时间字符串
        /// </summary>
        /// <returns></returns>
        public static string getNowDate()
        {
            return DateTime.Now.ToString();
        }
        /// <summary>
        /// 获取指定时间字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string getAssignDate(DateTime dt)
        {
            return dt.ToString();
        }
        /// <summary>
        /// 获取Sqlite用当前时间的字符串
        /// </summary>
        /// <returns></returns>
        public static string getSqliteDate()
        {
            return DateTime.Now.ToString("s");
        }
        /// <summary>
        /// 获取sqlite用指定时间的字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string getAssignSqliteDate(DateTime dt)
        {
            return dt.ToString("s");
        }
        #endregion


        #region ArrayList操作
        /// <summary>
        /// 查看ArrayList中是否包含指定的字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="al"></param>
        /// <returns></returns>
        public static Boolean ChkISinArray(string str, ArrayList al)
        {
            bool flag = false;
            foreach (string strname in al)
            {
                if (strname.Trim() == str.Trim())
                {
                    flag = true;
                    break;
                }
            }
            return flag;
        }



        /// <summary>
        /// 将字符串转换成ArrayList
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static ArrayList String2ArrayList(string str)
        {
            ArrayList al = new ArrayList();
            if (!str.Contains(","))
            {
                al.Add(str);
            }
            else
            {
                string[] strs = str.Split(',');
                foreach (string strsingle in strs)
                {
                    if (strsingle.Trim().Length > 0)
                    {
                        al.Add(strsingle.Trim());
                    }
                }
            }
            return al;
        }
        #endregion


    }
}